package cn.itsource.hrm.service.impl;

import cn.itsource.dto.SMSSendLogDTO;
import cn.itsource.hrm.client.AuthServiceClient;
import cn.itsource.hrm.domain.LoginUser;
import cn.itsource.hrm.domain.VipBase;
import cn.itsource.hrm.domain.VipUser;
import cn.itsource.hrm.dto.LoginUserDto;
import cn.itsource.hrm.dto.UserRegisterDTO;
import cn.itsource.hrm.exception.Assert;
import cn.itsource.hrm.exception.BusinessException;
import cn.itsource.hrm.mapper.VipUserMapper;
import cn.itsource.hrm.service.IVipBaseService;
import cn.itsource.hrm.service.IVipUserService;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.BitStatesUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author yaosang
 * @since 2022-04-13
 */
@Service
public class VipUserServiceImpl extends ServiceImpl<VipUserMapper, VipUser> implements IVipUserService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private AuthServiceClient authServiceClient;

    @Autowired
    private IVipBaseService vipBaseService;

    /**
* 1.参数校验，
* 2.条件校验(手机验证码得对 , 手机号查重)
* 3.保存login , 返回login的ID
* 3.1.用户中心集成Fegin
* 3.2.注入认证中心的Fegin，保存Login
*
* 4.保存vip_user,设置loginid
* 5.保存vip_base
     * @param dto ：参数对象
     */
    @Override
    @Transactional
    public void register(UserRegisterDTO dto) {
        //* 1.参数校验，
        Assert.assertPhone(dto.getMobile(),"无效的手机号");
        //* 2.条件校验(手机验证码得对 , 手机号查重)
        String smsCodeKey =  "sms:"+dto.getMobile();
        SMSSendLogDTO smsSendLogDTO = (SMSSendLogDTO) redisTemplate.opsForValue().get(smsCodeKey);
        Assert.assertNotNull(smsSendLogDTO ,"无效的验证码或过期" );
        if(!smsSendLogDTO.getSmsCode().equals(dto.getSmsCode())){
            throw new BusinessException("手机验证码错误");
        }
        //手机号查重
        Wrapper<VipUser> wrapper = new EntityWrapper<>();
        wrapper.eq("phone",dto.getMobile() );
        VipUser vipUserFromDB = selectOne(wrapper);
        Assert.assertNull(vipUserFromDB , "手机号已经被注册");

        //* 3.保存login , 返回login的ID
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(dto.getMobile() );
        loginUser.setPassword(dto.getPassword());
        loginUser.setType(LoginUser.USER_TYPE_WEBSITE);

        //* 3.1.用户中心集成Fegin
        //* 3.2.注入认证中心的Fegin，保存Login
        AjaxResult ajaxResult = authServiceClient.add(new LoginUserDto(loginUser, null));
        Assert.assertTrue(ajaxResult.isSuccess(),"登录信息保存失败");
        Assert.assertNotNull(ajaxResult.getResultObj(),"登录信息保存失败");
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());
        //* 4.保存vip_user,设置loginid
        VipUser vipUser = new VipUser();
        vipUser.setCreateTime(new Date().getTime());
        vipUser.setPhone(dto.getMobile());
        vipUser.setNickName(dto.getMobile());
        vipUser.setLoginId(loginId);

        //注册成功
        long bitState = BitStatesUtils.addState(0l, BitStatesUtils.OP_REGISTED);
        //账号激活
        bitState = BitStatesUtils.addState(bitState, BitStatesUtils.OP_ACTIVED);
        //手机认证
        bitState = BitStatesUtils.addState(bitState, BitStatesUtils.OP_AUTHED_PHONE);
        vipUser.setBitState(bitState);
        insert(vipUser);

        //* 5.保存vip_base
        VipBase vipBase = new VipBase();
        vipBase.setCreateTime(vipUser.getCreateTime());
        vipBase.setSsoId(vipUser.getId());
        //注册渠道
        vipBase.setRegChannel(dto.getRegChannel());
        vipBase.setRegTime(vipUser.getCreateTime());
        vipBaseService.insert(vipBase);
    }
}
